<template>
    <el-card>
      <h2>城市列表</h2>
      <el-table :data="cities" style="width: 100%">
        <el-table-column prop="city_name" label="城市" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="setDefaultCity(scope.row.city_id)">设为默认</el-button>
            <el-button type="danger" @click="deleteCity(scope.row.city_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-input v-model="newCityName" placeholder="输入城市名称" />
      <el-button @click="addCity">添加城市</el-button>
    </el-card>
  </template>
  
  <script>
  export default {
    computed: {
      cities() {
        return this.$store.state.cities
      }
    },
    data() {
      return {
        newCityName: ''
      }
    },
    methods: {
      addCity() {
        this.$store.dispatch('addCity', this.newCityName)
      },
      deleteCity(cityId) {
        this.$store.dispatch('deleteCity', cityId)
      },
      setDefaultCity(cityId) {
        this.$store.dispatch('setDefaultCity', cityId)
      }
    },
    created() {
      this.$store.dispatch('fetchCities')
    }
  }
  </script>